<template>
	<view class="addInfo bg-white">
		<u-form label-position='top'>
			<u-form-item label="真实姓名"><u-input v-model="name" /></u-form-item>
			<u-form-item label="身份证"><u-input v-model="card" /></u-form-item>
			<u-form-item label="手机号"><u-input v-model="phone" /></u-form-item>
			<u-button v-if="!id" @click='$u.debounce(save,500)'>保存</u-button>
			<u-button v-if="id" @click='$u.debounce(del,500)'>删除</u-button>
		</u-form>
	</view>
</template>

<script>
	import {addGuest,deleteGuest} from '@/api/api.js'
	export default {
		data() {
			return {
				name:"",
				card:"",
				phone:'',
				id:null
			};
		},
		onLoad({id,guestName,guestPhone,guestCard}) {
			this.name=guestName;
			this.id=id;
			this.card = guestCard;
			this.phone=guestPhone;
		},
		methods:{
			del(){
				deleteGuest(this.id).then(res=>{
					uni.showToast({
						title:"删除成功"
					});
					this.id=null;
					this.name="";
					this.card="";
					this.phone="";
					uni.$emit("addPeople")
				})
			},
			save(){
				if(this.$u.test.isEmpty(this.name)){
					uni.showToast({
						icon:"none",
						title:"姓名不能为空"
					})
					return;
				}
				if(!this.$u.test.idCard(this.card)){
					uni.showToast({
						icon:"none",
						title:"请输入正确的身份证号"
					})
					return;
				}
				if(!this.$u.test.mobile(this.phone)){
					uni.showToast({
						icon:"none",
						title:"请输入正确的手机号"
					})
					return;
				}
				addGuest({
					guestName:this.name,
					guestPhone:this.phone,
					guestCard:this.card
				}).then(res=>{
					if(res.code==200){
						uni.$emit("addPeople");
						uni.navigateBack();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.addInfo {
		padding: 0 30rpx;
		height: 100vh;
	}
</style>
